package com.watermeter.android.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.watermeter.android.R;
import com.watermeter.android.adapter.CurstorAdapter;
import com.watermeter.android.entry.CrutorJlu;
import com.watermeter.android.utils.UConstants;
import com.watermeter.android.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.List;


/**
 * 在线查询
 *
 * @author -zouqh
 */
public class OlneSelectActivity extends Activity implements OnClickListener {

    private ImageView close;
    private TextView title;
    private EditText input;
    private TextView select;
    private ListView listView;
    ProgressDialog dialog;
    Util util;
    private List<CrutorJlu> crutorJluList;
    private CurstorAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.olne_select_activity);
        close = (ImageView) findViewById(R.id.close);
        title = (TextView) findViewById(R.id.title_text);
        input = (EditText) findViewById(R.id.searchCru);
        select = (TextView) findViewById(R.id.sureSelect);
        listView = (ListView) findViewById(R.id.listview);
        title.setText("在线查询");
        close.setOnClickListener(this);
        select.setOnClickListener(this);
        util = new Util(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent intent;
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;

            case R.id.sureSelect:
                if (input.getText().toString().trim().length() <= 0) {
                    Toast.makeText(this, "请输客户代码", Toast.LENGTH_SHORT).show();
                } else {
                    getDate(input.getText().toString());
                }
                break;

            default:
                break;
        }
    }

    //查找客户历史记录
    private void getDate(final String userId) {
        dialog = ProgressDialog.show(this, null, "加载中...", true, false);
        RequestParams params = new RequestParams(UConstants.GET_CURTOR);
        params.addBodyParameter("userId", userId);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
//                        String msg = object.getString("msg");
                        if (success) {
                            Gson gson = new Gson();
                            Type lt = new TypeToken<List<CrutorJlu>>() {
                            }.getType();
                            crutorJluList = gson.fromJson(object.getString("rows").toString(), lt);
//                            getJson(object.getString("rows").toString());
                            if (crutorJluList != null) {
                                adapter = new CurstorAdapter(OlneSelectActivity.this, crutorJluList);
                                listView.setAdapter(adapter);

                            } else {
                                Toast.makeText(x.app(), "客户暂无记录！", Toast.LENGTH_LONG).show();
                            }

                        } else {
                            Toast.makeText(x.app(), "没找到该客户！", Toast.LENGTH_LONG).show();
                        }
//                        Toast.makeText(x.app(), msg, Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                dialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "获取数据失败！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }


}
